<?php
/**
 * author: zhypy<214681832@qq.com>
 * Date: 2020/7/2
 */
declare (strict_types=1);

namespace app\services\user;

use app\services\activity\StoreBargainServices;
use app\services\activity\StoreCombinationServices;
use app\services\activity\StoreSeckillServices;
use app\services\BaseServices;
use app\dao\user\UserDao;
use app\services\coupon\StoreCouponUserServices;
use app\services\message\service\StoreServiceServices;
use app\services\activity\StorePinkServices;
use app\services\order\StoreOrderServices;
use app\services\other\QrcodeServices;
use app\services\product\product\StoreProductRelationServices;
use app\services\system\attachment\SystemAttachmentServices;
use app\services\system\SystemUserLevelServices;
use app\services\wechat\WechatUserServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Route as Url;

/**
 *
 * Class UserServices
 * @package app\services\user
 * @method array getUserInfoArray(array $where, string $field, string $key) 根据条件查询对应的用户信息以数组形式返回
 * @method update($id, array $data, ?string $key = null) 修改数据
 * @method get(int $id) 获取一条数据
 * @method count(array $where) 获取指定条件下的数量
 * @method value(array $where, string $field) 获取指定的键值
 * @method bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
 * @method bcDec($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度减法
 */
class UserServices extends BaseServices
{

    /**
     * UserServices constructor.
     * @param UserDao $dao
     */
    public function __construct(UserDao $dao) {
        $this->dao = $dao;
    }

    /**
     * 获取用户信息
     * @param $id
     * @param $field
     */
    public function getUserInfo(int $uid, $field = '*') {
        if (is_string($field)) $field = explode(',', $field);
        return $this->dao->get($uid, $field);
    }

    /**
     * 获取用户列表
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserList(array $where, string $field): array {
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getList($where, $field, $page, $limit);
        $count = $this->getCount($where);
        return compact('list', 'count');
    }

    public function getSniperUid()
    {
        $list = $this->dao->getSniper();
        return array_column($list, 'uid');
    }

    /**
     * 列表条数
     * @param array $where
     * @return int
     */
    public function getCount(array $where, bool $is_list = false) {
        return $this->dao->getCount($where, $is_list);
    }

    /**
     * 保存用户信息
     * @param $user
     * @param int $spreadUid
     * @param string $userType
     * @return User|\think\Model
     */
    public function setUserInfo($user, int $spreadUid = 0, string $userType = 'wechat') {
        $res = $this->dao->save([
            'account' => $user['account'] ?? 'wx' . rand(1, 9999) . time(),
            'pwd' => $user['pwd'] ?? md5('123456'),
            'nickname' => $user['nickname'] ?? '',
            'avatar' => $user['headimgurl'] ?? '',
            'phone' => $user['phone'] ?? '',
            'spread_uid' => $spreadUid,
            'add_time' => time(),
            'add_ip' => app()->request->ip(),
            'last_time' => time(),
            'last_ip' => app()->request->ip(),
            'user_type' => $userType
        ]);
        if (!$res)
            throw new AdminException('保存用户信息失败');
        /** @var UserBillServices $userBill */
        $userBill = app()->make(UserBillServices::class);
        //邀请新用户增加经验
        $userBill->inviteUserIncExp((int)$spreadUid);
        return $res;
    }

    public function getUidByKeyword($keyword) {
        return $this->dao->getUserSimple(['account|self_inv_code' => $keyword], 'uid');
    }

    /**
     * 某些条件用户佣金总和
     * @param array $where
     * @return mixed
     */
    public function getSumBrokerage(array $where) {
        return $this->dao->getWhereSumField($where, 'brokerage_price');
    }

    /**
     * 根据条件获取用户指定字段列表
     * @param array $where
     * @param string $field
     * @param string $key
     * @return array
     */
    public function getColumn(array $where, string $field = '*', string $key = '') {
        return $this->dao->getColumn($where, $field, $key);
    }

    /**
     * 获取某个用户的推广下线
     */
    public function getSpreadList($uid) {
        $one_uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
        $two_uids = $this->dao->getColumn([['spread_uid', 'in', $one_uids], ['spread_uid', '<>', 0]], 'uid');
        $uids = array_merge($one_uids, $two_uids);
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getList(['uid' => $uids], 'uid,nickname,real_name,avatar,add_time', $page, $limit);
        foreach ($list as $k => $user) {
            $list[$k]['type'] = in_array($user['uid'], $one_uids) ? '一级' : '二级';
            $list[$k]['add_time'] = date('Y-m-d', $user['add_time']);
        }
        $count = count($uids);
        return compact('count', 'list');
    }

    /**查找多个uid信息
     * @param $uids
     * @param bool $field
     * @return UserDao|bool|\crmeb\basic\BaseModel|mixed|\think\Collection
     */
    public function getUserListByUids($uids, $field = false) {
        if (!$uids || !is_array($uids)) return false;
        return $this->dao->getUserListByUids($uids, $field);
    }

    /**
     * 获取分销用户
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAgentUserList(array $where = [], string $field = '*') {
        $where['status'] = 1;
        $where['is_promoter'] = 1;
        if (isset($where['nickname']) && $where['nickname'] !== '') {
            $where['like'] = $where['nickname'];
        }
        if (isset($where['data']) && $where['data']) {
            $where['time'] = $where['data'];
        }
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getAgentUserList($where, $field, $page, $limit);
        $count = $this->dao->count($where);
        return compact('count', 'list');
    }

    /**
     * 获取分销员ids
     * @param array $where
     * @return array
     */
    public function getAgentUserIds(array $where) {
        $where['status'] = 1;
        $where['is_promoter'] = 1;
        if (isset($where['nickname']) && $where['nickname'] !== '') {
            $where['like'] = $where['nickname'];
        }
        if (isset($where['data']) && $where['data']) {
            $where['time'] = $where['data'];
        }
        return $this->dao->getAgentUserIds($where);
    }

    /**
     * 获取推广人列表
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSairList(array $where, string $field = '*') {
        $where_data = [];
        if (isset($where['uid'])) {
            if (isset($where['type'])) {
                $uids = $this->getColumn(['spread_uid' => $where['uid']], 'uid');
                switch ((int)$where['type']) {
                    case 1:
                        $where_data['uid'] = count($uids) > 0 ? $uids : 0;
                    case 2:
                        if (count($uids))
                            $spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid');
                        else
                            $spread_uid_two = [];
                        $where_data['uid'] = count($spread_uid_two) > 0 ? $spread_uid_two : 0;
                        break;
                    default:
                        if (count($uids)) {
                            if ($spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid')) {
                                $uids = array_merge($uids, $spread_uid_two);
                                $uids = array_unique($uids);
                                $uids = array_merge($uids);
                            }
                        }
                        $where_data['uid'] = count($uids) > 0 ? $uids : 0;
                        break;
                }
            }
            if (isset($where['data']) && $where['data']) {
                $where_data['time'] = $where['data'];
            }
            if (isset($where['nickname']) && $where['nickname']) {
                $where_data['like'] = $where['nickname'];
            }
            $where_data['status'] = 1;
        }
        [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getSairList($where_data, '*', $page, $limit);
        $count = $this->dao->count($where_data);
        return compact('list', 'count');
    }

    /**
     * 获取推广人统计
     * @param array $where
     * @param string $field
     * @param int $page
     * @param int $limit
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSairCount(array $where) {
        $where_data = [];
        if (isset($where['uid'])) {
            if (isset($where['type'])) {
                $uids = $this->getColumn(['spread_uid' => $where['uid']], 'uid');
                switch ((int)$where['type']) {
                    case 1:
                        $where_data['uid'] = count($uids) > 0 ? $uids : 0;
                    case 2:
                        if (count($uids))
                            $spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid');
                        else
                            $spread_uid_two = [];
                        $where_data['uid'] = count($spread_uid_two) > 0 ? $spread_uid_two : 0;
                        break;
                    default:
                        if (count($uids)) {
                            if ($spread_uid_two = $this->dao->getColumn([['spread_uid', 'IN', $uids]], 'uid')) {
                                $uids = array_merge($uids, $spread_uid_two);
                                $uids = array_unique($uids);
                                $uids = array_merge($uids);
                            }
                        }
                        $where_data['uid'] = count($uids) > 0 ? $uids : 0;
                        break;
                }
            }
            if (isset($where['data']) && $where['data']) {
                $where_data['time'] = $where['data'];
            }
            if (isset($where['nickname']) && $where['nickname']) {
                $where_data['like'] = $where['nickname'];
            }
            $where_data['status'] = 1;
        }
        return $this->dao->count($where_data);
    }

    /**
     * 写入用户信息
     * @param array $data
     * @return bool
     */
    public function create(array $data) {
        if (!$this->dao->save($data))
            throw new AdminException('写入失败');
        return true;
    }

    /**
     * 重置密码
     * @param $id
     * @param string $password
     * @return mixed
     */
    public function resetPwd(int $uid, string $password) {
        if (!$this->dao->update($uid, ['pwd' => $password]))
            throw new AdminException('密码重置失败');
        return true;
    }

    /**
     * 增加推广人数
     * @param int $uid
     * @param int $num
     * @return bool
     * @throws Exception
     */
    public function incSpreadCount(int $uid, int $num = 1) {
        if (!$this->dao->incField($uid, 'spread_count', $num))
            throw new Exception('增加推广人数失败');
        return true;
    }


    /**
     * 设置用户登录类型
     * @param int $uid
     * @param string $type
     * @return bool
     * @throws Exception
     */
    public function setLoginType(int $uid, string $type = 'h5') {
        if (!$this->dao->update($uid, ['login_type' => $type]))
            throw new Exception('设置登录类型失败');
        return true;
    }

    /**
     * 设置推广员
     * @param int $uid
     * @param int $is_promoter
     * @return bool
     * @throws Exception
     */
    public function setIsPromoter(int $uid, $is_promoter = 1) {
        if (!$this->dao->update($uid, ['is_promoter' => $is_promoter]))
            throw new Exception('设置推广员失败');
        return true;
    }

    /**
     * 设置用户分组
     * @param $uids
     * @param int $group_id
     */
    public function setUserGroup($uids, int $group_id) {
        return $this->dao->batchUpdate($uids, ['group_id' => $group_id], 'uid');
    }

    /**
     * 增加用户余额
     * @param int $uid
     * @param float $old_now_money
     * @param float $now_money
     * @return bool
     * @throws Exception
     */
    public function addNowMoney(int $uid, $old_now_money, $now_money) {
        if (!$this->dao->update($uid, ['now_money' => bcadd($old_now_money, $now_money, 2)]))
            throw new Exception('增加用户余额失败');
        return true;
    }

    /**
     * 减少用户余额
     * @param int $uid
     * @param float $old_now_money
     * @param float $now_money
     * @return bool
     * @throws Exception
     */
    public function cutNowMoney(int $uid, $old_now_money, $now_money) {
        if ($old_now_money > $now_money) {
            if (!$this->dao->update($uid, ['now_money' => bcsub($old_now_money, $now_money, 2)]))
                throw new Exception('减少用户余额失败');
        }
        return true;
    }

    /**
     * 增加用户佣金
     * @param int $uid
     * @param float $brokerage_price
     * @param float $price
     * @return bool
     * @throws Exception
     */
    public function addBrokeragePrice(int $uid, $brokerage_price, $price) {
        if (!$this->dao->update($uid, ['brokerage_price' => bcadd($brokerage_price, $price, 2)]))
            throw new Exception('增加用户佣金失败');
        return true;
    }

    /**
     * 减少用户佣金
     * @param int $uid
     * @param float $brokerage_price
     * @param float $price
     * @return bool
     * @throws Exception
     */
    public function cutBrokeragePrice(int $uid, $brokerage_price, $price) {
        if (!$this->dao->update($uid, ['brokerage_price' => bcsub($brokerage_price, $price, 2)]))
            throw new Exception('减少用户佣金失败');
        return true;
    }

    /**
     * 增加用户积分
     * @param int $uid
     * @param float $old_integral
     * @param float $integral
     * @return bool
     * @throws Exception
     */
    public function addIntegral(int $uid, $old_integral, $integral) {
        if (!$this->dao->update($uid, ['integral' => bcadd($old_integral, $integral, 2)]))
            throw new Exception('增加用户积分失败');
        return true;
    }

    /**
     * 减少用户积分
     * @param int $uid
     * @param float $old_integral
     * @param float $integral
     * @return bool
     * @throws Exception
     */
    public function cutIntegral(int $uid, $old_integral, $integral) {
        if (!$this->dao->update($uid, ['integral' => bcsub($old_integral, $integral, 2)]))
            throw new Exception('减少用户积分失败');
        return true;
    }

    /**
     * 增加用户经验
     * @param int $uid
     * @param float $old_exp
     * @param float $exp
     * @return bool
     * @throws Exception
     */
    public function addExp(int $uid, float $old_exp, float $exp) {
        if (!$this->dao->update($uid, ['exp' => bcadd($old_exp, $exp, 2)]))
            throw new Exception('增加用户经验失败');
        return true;
    }

    /**
     * 减少用户经验
     * @param int $uid
     * @param float $old_exp
     * @param float $exp
     * @return bool
     * @throws Exception
     */
    public function cutExp(int $uid, float $old_exp, float $exp) {
        if (!$this->dao->update($uid, ['exp' => bcsub($old_exp, $exp, 2)]))
            throw new Exception('减少用户经验失败');
        return true;
    }

    public function getUser($where = [])
    {
        return $this->dao->getUserSimple($where);
    }

    public function getUsers($ids)
    {
        return $this->dao->getUsersByIds($ids);
    }

    /**
     * 获取用户标签
     * @param $uid
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserLablel(array $uids) {
        /** @var UserLabelRelationServices $services */
        $services = app()->make(UserLabelRelationServices::class);
        $userlabels = $services->getUserLabelList($uids);
        $data = [];
        foreach ($uids as $uid) {
            $labels = array_filter($userlabels, function ($item) use ($uid) {
                if ($item['uid'] == $uid) {
                    return true;
                }
            });
            $data[$uid] = implode(',', array_column($labels, 'label_name'));
        }
        return $data;
    }

    /**
     * 显示资源列表头部
     * @return array[]
     */
    public function typeHead() {
        //全部会员
        $all = $this->getCount([]);
        /** @var UserWechatuserServices $userWechatUser */
        $userWechatUser = app()->make(UserWechatuserServices::class);
        //小程序会员
        $routine = $userWechatUser->getCount([['w.user_type', '=', 'routine']]);
        //公众号会员
        $wechat = $userWechatUser->getCount([['w.user_type', '=', 'wechat']]);
        //H5会员
        $h5 = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'h5']);
        return [
            ['user_type' => '', 'name' => '全部会员', 'count' => $all],
            ['user_type' => 'routine', 'name' => '小程序会员', 'count' => $routine],
            ['user_type' => 'wechat', 'name' => '公众号会员', 'count' => $wechat],
            ['user_type' => 'h5', 'name' => 'H5会员', 'count' => $h5],
        ];
    }

    /**
     * 会员列表
     * @param array $where
     * @return array
     */
    public function index(array $where) {
        /** @var UserWechatuserServices $userWechatUser */
        $userWechatUser = app()->make(UserWechatuserServices::class);
        $fields = 'u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.user_type as w_user_type,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time';
        [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
        if ($list) {
            $uids = array_column($list, 'uid');
            $userlabel = $this->getUserLablel($uids);
            $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
            $userExtract = app()->make(UserExtractServices::class)->getUsersSumList($uids);
            $levelName = app()->make(SystemUserLevelServices::class)->getUsersLevel(array_unique(array_column($list, 'level')));
            $userLevel = app()->make(UserLevelServices::class)->getUsersLevelInfo($uids);
            foreach ($list as &$item) {
                $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
                $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
                $item['extract_count_price'] = $userExtract[$item['uid']] ?? 0;//累计提现
                //$item['spread_uid_nickname'] = $item['spread_uid'] ? ($item['spreadUser']['nickname'] ?? '') . '/' . $item['spread_uid'] : '无';
                //修改推荐人显示
                $inv_code = $item['inv_code'];
                $item['spread_uid_nickname'] = $inv_code == 951753 ? '默认推荐人' : get_inv_nickname($inv_code);
                //用户类型
                if ($item['openid'] != '' && $item['w_user_type'] == 'routine') {
                    $item['user_type'] = '小程序';
                } else if ($item['openid'] != '' && $item['w_user_type'] == 'wechat') {
                    $item['user_type'] = '公众号';
                } else if ($item['user_type'] == 'h5') {
                    $item['user_type'] = 'H5';
                } else $item['user_type'] = '其他';
                if ($item['sex'] == 1) {
                    $item['sex'] = '男';
                } else if ($item['sex'] == 2) {
                    $item['sex'] = '女';
                } else $item['sex'] = '保密';
                //等级名称
                $item['level'] = $levelName[$item['level']] ?? '无';
                //分组名称
                $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
                //用户等级
                $item['vip_name'] = false;
                $levelinfo = $userLevel[$item['uid']] ?? null;
                if ($levelinfo) {
                    if ($levelinfo && ($levelinfo['is_forever'] || time() < $levelinfo['valid_time'])) {
                        $item['vip_name'] = $item['level'] != '无' ? $item['level'] : false;
                    }
                }
                $item['labels'] = $userlabel[$item['uid']] ?? '';
            }
        }

        //如果设置了时间间隔使用时间间隔，如果没有设置，则计算1970至今
        $start = 0;
        $start_time_str = "1970-01-01 00:00:00";
        $end = time();
        $end_time_str = date('Y-m-d H:i:s');
        if (!empty($where['user_time'])) {
            $arr = explode('-', $where['user_time']);
            $start_arr = array_slice($arr, 0, 3);
            $start = strtotime(implode('-', $start_arr));
            $start_time_str = date('Y-m-d H:i:s', $start);
            $end_arr = array_slice($arr, 3, 3);
            $end = strtotime(implode('-', $end_arr));
            $end_time_str = date('Y-m-d H:i:s', $end);
        }
        foreach ($list as $key => $value) {
            $uid = $value['uid'];
            //统计充值
            $cz_tongji = Db::table('eb_ptpay_recharge')->where('param', $uid)
                ->where('really_price', '>', 0)
                ->whereBetweenTime('created_at', $start_time_str, $end_time_str)
                ->sum('really_price');
            $list[$key]['cz_tongji'] = number_format($cz_tongji, 2);
            //统计提现
            $tx_tongji = Db::table('eb_user_extract')->where('uid', $uid)
                ->where('status', 1)
                ->where('add_time', '>', $start)
                ->where('add_time', '<', $end)
                ->sum('extract_price');
            $list[$key]['tx_tongji'] = number_format($tx_tongji, 2);
        }

        return compact('count', 'list');
    }

    public function getAgentUser($date)
    {
        /** @var UserWechatuserServices $userWechatUser */
        $userWechatUser = app()->make(UserWechatuserServices::class);
        $fields = 'u.*';
        $where['is_agent'] = 1;
        [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
        foreach ($list as &$item) {
            $item['spread_uid_nickname'] = $item['inv_code'] == 951753 ? '默认推荐人' : get_inv_nickname($item['inv_code']);
            $item['report'] = $this->getAgentUserReport($item['agent_addr'], $item['agent_time'], $date);
            $item['agent_date'] = date('Y-m-d H:i:s', $item['agent_time']);
        }
        return compact('count', 'list');
    }

    private function getAgentUserReport($addr, $time, $date)
    {
        if (!empty($date)) {
            if ($date['start'] < $time) {
                $date['start'] = $time;
            }
        } else {
            $date['start'] = $time;
            $date['end'] = time();
        }

        return Db::table('eb_store_order')
            ->where('user_address', 'LIKE', "{$addr}%")
            ->where('pink_id', '>', 0)
            ->where('pink_type', '>', 0)
            ->where('pink_type', '>', 0)
            ->where('add_time', '>=', $date['start'])
            ->where('add_time', '<=', $date['end'])
            ->sum('total_price');
    }

    public function getExportData($where)
    {
        $list = Db::table('eb_user')->where($where)->select()->toArray();
        foreach ($list as &$item) {
            //修改推荐人显示
            $inv_code = $item['inv_code'];
            $item['spread_uid_nickname'] = $inv_code == 951753 ? '默认推荐人' : get_inv_nickname($inv_code);
            //用户类型
            if ($item['user_type'] == 'wechat') {
                $item['user_type'] = '公众号';
            } else if ($item['user_type'] == 'h5') {
                $item['user_type'] = 'H5';
            } else $item['user_type'] = '其他';
            $uid = $item['uid'];
            //统计充值
            //如果设置了时间间隔使用时间间隔，如果没有设置，则计算1970至今
            $start = 0;
            $start_time_str = "1970-01-01 00:00:00";
            $end = time();
            $end_time_str = date('Y-m-d H:i:s');
            if (!empty($where['user_time'])) {
                $arr = explode('-', $where['user_time']);
                $start_arr = array_slice($arr, 0, 3);
                $start = strtotime(implode('-', $start_arr));
                $start_time_str = date('Y-m-d H:i:s', $start);
                $end_arr = array_slice($arr, 3, 3);
                $end = strtotime(implode('-', $end_arr));
                $end_time_str = date('Y-m-d H:i:s', $end);
            }
            $cz_tongji = Db::table('eb_ptpay_recharge')->where('param', $uid)
                ->where('really_price', '>', 0)
                ->whereBetweenTime('created_at', $start_time_str, $end_time_str)
                ->sum('really_price');
            $item['cz_tongji'] = number_format($cz_tongji, 2);
            //统计提现
            $tx_tongji = Db::table('eb_user_extract')->where('uid', $uid)
                ->where('status', 1)
                ->where('add_time', '>', $start)
                ->where('add_time', '<', $end)
                ->sum('extract_price');
            $item['tx_tongji'] = number_format($tx_tongji, 2);
        }
        return $list;
    }

    /**
     * 获取修改页面数据
     * @param int $id
     * @return array
     * @throws \FormBuilder\exception\FormBuilderException
     */
    public function edit(int $id) {
        $user = $this->getUserInfo($id);
        if (!$user)
            throw new AdminException('数据不存在');
        $f = array();
        $f[] = Form::input('uid', '用户编号', $user->getData('uid'))->disabled(true);
        $f[] = Form::input('real_name', '真实姓名', $user->getData('real_name'));
        $f[] = Form::input('phone', '手机号码', $user->getData('phone'));
        $f[] = Form::date('birthday', '生日', $user->getData('birthday') ? date('Y-m-d', $user->getData('birthday')) : '');
        $f[] = Form::input('card_id', '身份证号', $user->getData('card_id'));
        $f[] = Form::input('addres', '用户地址', $user->getData('addres'));
        $f[] = Form::textarea('mark', '用户备注', $user->getData('mark'));
        $f[] = Form::number('start_num', '发团点', $user->getData('start_num'));
        $f[] = Form::number('points', '积分', $user->getData('points'));
        $f[] = Form::number('now_money', '余额', $user->getData('now_money'));

        //查询高于当前会员的所有会员等级
        $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
        $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>=', $grade != '' ? $grade : 0], 'id,name');
//        $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', 0], 'id,name');
        $f[] = Form::select('level', '会员等级', (string)$user->getData('level'))->setOptions(function () use ($systemLevelList) {
            $menus = [];
            foreach ($systemLevelList as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
            }
            return $menus;
        })->filterable(true);
        $systemGroupList = app()->make(UserGroupServices::class)->getGroupList();
        $f[] = Form::select('group_id', '用户分组', (string)$user->getData('group_id'))->setOptions(function () use ($systemGroupList) {
            $menus = [];
            foreach ($systemGroupList as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
            }
            return $menus;
        })->filterable(true);
        $systemLabelList = app()->make(UserLabelServices::class)->getLabelList();
        $labels = app()->make(UserLabelRelationServices::class)->getUserLabels($user['uid']);
        $f[] = Form::select('label_id', '用户标签', $labels)->setOptions(function () use ($systemLabelList) {
            $menus = [];
            foreach ($systemLabelList as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
            }
            return $menus;
        })->filterable(true)->multiple(true);
        $f[] = Form::radio('is_promoter', '推广员', $user->getData('is_promoter'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]);
        $f[] = Form::radio('status', '状态', $user->getData('status'))->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '锁定']]);
        return create_form('编辑', $f, Url::buildUrl('/user/user/' . $id), 'PUT');
    }

    /**
     * 修改提交处理
     * @param $id
     * @return mixed
     */
    public function updateInfo(int $id, array $data) {
        $user = $this->getUserInfo($id);
        if (!$user) {
            throw new AdminException('数据不存在!');
        }
        $res1 = true;
        $res2 = true;
        $res3 = true;
        $edit = [];
        $add_time = time();
        if ((float)$data['now_money'] != (float)$user['now_money']) {
            /** @var UserBillServices $userBill */
            $userBill = app()->make(UserBillServices::class);
            $bill_data = [
                'link_id' => $data['adminId'] ?? 0,
                'balance' => $data['now_money']
            ];
            $edit['now_money'] = $data['now_money'];
            $money = (float)$data['now_money'] - (float)$user['now_money'];
            if ($money > 0) {
                $bill_data['number'] = $money;
                $bill_data['title'] = '系统增加余额';
                $bill_data['mark'] = '系统增加了' . floatval($money) . '余额';
                $res1 = $userBill->incomeNowMoney($user['uid'], 'system_add', $bill_data);
            } else {
                $bill_data['number'] = -$money;
                $bill_data['title'] = '系统减少余额';
                $bill_data['mark'] = '系统扣除了' . floatval(-$money) . '余额';
                $res1 = $userBill->expendNowMoney($user['uid'], 'system_sub', $bill_data);
            }
        }
        if ((int)$data['start_num'] != (int)$user['start_num']) {
            // TODO: 发团点明细
            $edit['start_num'] = $data['start_num'];
            $start_num = (float)$data['start_num'] - (float)$user['start_num'];
            if ($start_num > 0) {
                $addSql = "insert into eb_user_start_points (uid,link_id,pm,title,`number`,balance,mark,add_time) values 
({$user['uid']},9999,1,'系统增加发团点".$start_num."',$start_num,{$data['start_num']},'系统增加发团点',$add_time)";
            } else {
                $addSql = "insert into eb_user_start_points (uid,link_id,pm,title,`number`,balance,mark,add_time) values 
({$user['uid']},9999,0,'系统扣除发团点".-$start_num."',-$start_num,{$data['start_num']},'系统扣除发团点',$add_time)";
            }
            $res2 = Db::execute($addSql);
        }
        if ((int)$data['points'] != (int)$user['points']) {
            // TODO: 积分明细
            $edit['points'] = $data['points'];
            $start_num = (float)$data['points'] - (float)$user['points'];
            if ($start_num > 0) {
                $integralData = [
                    'uid' => $user['uid'],
                    'link_id' => 9999,
                    'type' => 10,
                    'pm' => 1,
                    'title' => '系统增加积分',
                    'number' => $start_num,
                    'balance' => (int)$data['points'],
                    'mark' => '系统增加积分' . $start_num
                ];

            } else {
                $integralData = [
                    'uid' => $user['uid'],
                    'link_id' => 9999,
                    'type' => 10,
                    'pm' => 0,
                    'title' => '系统扣除积分',
                    'number' => -$start_num,
                    'balance' => (int)$data['points'],
                    'mark' => '系统增加积分' . -$start_num
                ];
            }
            $res3 = Db::table('eb_user_integral_log')->save($integralData);
        }
        //修改基本信息
        if (!isset($data['is_other']) || !$data['is_other']) {
            app()->make(UserLabelRelationServices::class)->setUserLable([$id], $data['label_id']);
            $edit['status'] = $data['status'];
            $edit['real_name'] = $data['real_name'];
            $edit['card_id'] = $data['card_id'];
            $edit['birthday'] = strtotime($data['birthday']);
            $edit['mark'] = $data['mark'];
            $edit['is_promoter'] = $data['is_promoter'];
            $edit['level'] = $data['level'];
            $edit['phone'] = $data['phone'];
            $edit['addres'] = $data['addres'];
            $edit['group_id'] = $data['group_id'];
            app()->make(UserLevelServices::class)->setUserLevel((int)$user['uid'], (int)$data['level']);
        }

        if ($edit) {
            $res4 = $this->dao->update($id, $edit);
        } else $res4 = true;
        if ($res1 && $res2 && $res3 && $res4)
            return true;
        else throw new AdminException('修改失败');
    }

    public function setUserData($id, $edit)
    {
        return $this->dao->update($id, $edit);
    }

    /**
     * 编辑其他
     * @param $id
     * @return mixed
     * @throws \FormBuilder\exception\FormBuilderException
     */
    public function editOther($id) {
        $user = $this->getUserInfo($id);
        if (!$user) {
            throw new AdminException('数据不存在!');
        }
        $f = array();
        $f[] = Form::radio('money_status', '修改余额', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
        $f[] = Form::number('money', '余额', 0)->min(0);
        $f[] = Form::radio('integration_status', '修改积分', 1)->options([['value' => 1, 'label' => '增加'], ['value' => 2, 'label' => '减少']]);
        $f[] = Form::number('integration', '积分', 0)->min(0);
        return create_form('修改其他', $f, Url::buildUrl('/user/update_other/' . $id), 'PUT');
    }

    /**
     * 设置会员分组
     * @param $id
     * @return mixed
     */
    public function setGroup($uids) {
        $userGroup = app()->make(UserGroupServices::class)->getGroupList();
        if (count($uids) == 1) {
            $user = $this->getUserInfo($uids[0], ['group_id']);
            $field[] = Form::select('group_id', '用户分组', (string)$user->getData('group_id'))->setOptions(function () use ($userGroup) {
                $menus = [];
                foreach ($userGroup as $menu) {
                    $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
                }
                return $menus;
            })->filterable(true);
        } else {
            $field[] = Form::select('group_id', '用户分组')->setOptions(function () use ($userGroup) {
                $menus = [];
                foreach ($userGroup as $menu) {
                    $menus[] = ['value' => $menu['id'], 'label' => $menu['group_name']];
                }
                return $menus;
            })->filterable(true);
        }
        $field[] = Form::hidden('uids', implode(',', $uids));
        return create_form('设置用户分组', $field, Url::buildUrl('/user/save_set_group'), 'PUT');
    }

    /**
     * 保存会员分组
     * @param $id
     * @return mixed
     */
    public function saveSetGroup($uids, int $group_id) {
        /** @var UserGroupServices $userGroup */
        $userGroup = app()->make(UserGroupServices::class);
        if (!$userGroup->getGroup($group_id)) {
            throw new AdminException('该分组不存在');
        }
        if (!$this->setUserGroup($uids, $group_id)) {
            throw new AdminException('设置分组失败或无改动');
        }
        return true;
    }

    /**
     * 设置用户标签
     * @param $uids
     * @return mixed
     */
    public function setLabel($uids) {
        $userLabel = app()->make(UserLabelServices::class)->getLabelList();
        if (count($uids) == 1) {
            $lids = app()->make(UserLabelRelationServices::class)->getUserLabels($uids[0]);
            $field[] = Form::select('label_id', '用户标签', $lids)->setOptions(function () use ($userLabel) {
                $menus = [];
                foreach ($userLabel as $menu) {
                    $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
                }
                return $menus;
            })->filterable(true)->multiple(true);
        } else {
            $field[] = Form::select('label_id', '用户标签')->setOptions(function () use ($userLabel) {
                $menus = [];
                foreach ($userLabel as $menu) {
                    $menus[] = ['value' => $menu['id'], 'label' => $menu['label_name']];
                }
                return $menus;
            })->filterable(true)->multiple(true);
        }
        $field[] = Form::hidden('uids', implode(',', $uids));
        return create_form('设置用户标签', $field, Url::buildUrl('/user/save_set_label'), 'PUT');
    }

    /**
     * 保存用户标签
     * @return mixed
     */
    public function saveSetLabel($uids, $lable_id) {
        foreach ($lable_id as $id) {
            if (!app()->make(UserLabelServices::class)->getLable((int)$id)) {
                throw new AdminException('有标签不存在或被删除');
            }
        }
        /** @var UserLabelRelationServices $services */
        $services = app()->make(UserLabelRelationServices::class);
        if (!$services->setUserLable($uids, $lable_id)) {
            throw new AdminException('设置标签失败');
        }
        return true;
    }

    /**
     * 赠送会员等级
     * @param int $uid
     * @return mixed
     * */
    public function giveLevel($id) {
        if (!$this->getUserInfo($id)) {
            throw new AdminException('用户不存在');
        }
        //查询高于当前会员的所有会员等级
        $grade = app()->make(UserLevelServices::class)->getUerLevelInfoByUid($id, 'grade');
        $systemLevelList = app()->make(SystemUserLevelServices::class)->getWhereLevelList(['grade', '>', $grade ?? 0], 'id,name');

        $field[] = Form::select('level_id', '会员等级')->setOptions(function () use ($systemLevelList) {
            $menus = [];
            foreach ($systemLevelList as $menu) {
                $menus[] = ['value' => $menu['id'], 'label' => $menu['name']];
            }
            return $menus;
        })->filterable(true);
        return create_form('赠送会员', $field, Url::buildUrl('/user/save_give_level/' . $id), 'PUT');
    }

    /**
     * 执行赠送会员等级
     * @param int $uid
     * @return mixed
     * */
    public function saveGiveLevel(int $id, int $level_id) {
        if (!$this->getUserInfo($id)) {
            throw new AdminException('用户不存在');
        }
        //查询当前选择的会员等级
        $systemLevel = app()->make(SystemUserLevelServices::class)->getLevel($level_id);
        if (!$systemLevel) throw new AdminException('您选择赠送的会员等级不存在！');
        //检查是否拥有此会员等级
        $level = app()->make(UserLevelServices::class)->getWhereLevel(['uid' => $id, 'level_id' => $level_id, 'is_del' => 0], 'valid_time,is_forever');
        if ($level) {
            if ($level['is_forever']) {
                throw new AdminException('此用户已有该会员等级，无法再次赠送');
            } else {
                if (time() < $level['valid_time'])
                    throw new AdminException('此用户已有该会员等级，无法再次赠送');
            }
        }
        //保存会员信息
        if (!app()->make(UserLevelServices::class)->setUserLevel($id, $level_id)) {
            throw new AdminException('赠送失败');
        }
        return true;
    }

    /**
     * 清除会员等级
     * @paran int $uid
     * @paran boolean
     * */
    public function cleanUpLevel($uid) {
        if (!$this->getUserInfo($uid))
            throw new AdminException('用户不存在');
        /** @var UserLevelServices $services */
        $services = app()->make(UserLevelServices::class);
        return $this->transaction(function () use ($uid, $services) {
            $res = $services->delUserLevel($uid);
            if (!$res && !$this->dao->update($uid, ['clean_time' => time(), 'level' => 0], 'uid'))
                throw new AdminException('清除失败');
            return true;
        });
    }

    /**
     * 用户详细信息
     * @param $uid
     */
    public function getUserDetailed(int $uid, $userIfno = []) {
        /** @var UserAddressServices $userAddress */
        $userAddress = app()->make(UserAddressServices::class);
        $field = 'real_name,phone,province,city,district,detail,post_code';
        $address = $userAddress->getUserDefaultAddress($uid, $field);
        if (!$address) {
            $address = $userAddress->getUserAddressList($uid, $field);
            $address = $address[0] ?? [];
        }
        $userInfo = $this->getUserInfo($uid);
        return [
            ['name' => '默认收货地址', 'value' => $address ? '收货人:' . $address['real_name'] . '邮编:' . $address['post_code'] . ' 收货人电话:' . $address['phone'] . ' 地址:' . $address['province'] . ' ' . $address['city'] . ' ' . $address['district'] . ' ' . $address['detail'] : ''],
            ['name' => '手机号码', 'value' => $userInfo['phone']],
            ['name' => '姓名', 'value' => ''],
            ['name' => '微信昵称', 'value' => $userInfo['nickname']],
            ['name' => '头像', 'value' => $userInfo['avatar']],
            ['name' => '邮箱', 'value' => ''],
            ['name' => '生日', 'value' => ''],
            ['name' => '积分', 'value' => $userInfo['integral']],
            ['name' => '上级推广人', 'value' => $userInfo['spread_uid'] ? $this->getUserInfo($userInfo['spread_uid'], ['nickname'])['nickname'] ?? '' : ''],
            ['name' => '账户余额', 'value' => $userInfo['now_money']],
            ['name' => '佣金总收入', 'value' => app()->make(UserBillServices::class)->getBrokerageSum($uid)],
            ['name' => '提现总金额', 'value' => app()->make(UserExtractServices::class)->getUserExtract($uid)],
        ];

    }

    /**
     * 获取用户详情里面的用户消费能力和用户余额积分等
     * @param $uid
     * @return array[]
     */
    public function getHeaderList(int $uid, $userInfo = []) {
        if (!$userInfo) {
            $userInfo = $this->getUserInfo($uid);
        }
        /** @var StoreOrderServices $orderServices */
        $orderServices = app()->make(StoreOrderServices::class);
        return [
            [
                'title' => '余额',
                'value' => $userInfo['now_money'] ?? 0,
                'key' => '元',
            ],
            [
                'title' => '总计订单',
                'value' => $orderServices->count(['uid' => $uid]),
                'key' => '笔',
            ],
            [
                'title' => '总消费金额',
                'value' => $orderServices->together(['uid' => $uid], 'total_price'),
                'key' => '元',
            ],
            [
                'title' => '积分',
                'value' => $userInfo['integral'] ?? 0,
                'key' => '',
            ],
            [
                'title' => '本月订单',
                'value' => $orderServices->count(['uid' => $uid, 'time' => 'month']),
                'key' => '笔',
            ],
            [
                'title' => '本月消费金额',
                'value' => $orderServices->together(['uid' => $uid, 'time' => 'month'], 'total_price'),
                'key' => '元',
            ],
            [
                'title' => '总业绩',
                'value' => $this->getUserAllConsume($uid),
                'key' => '元',
            ]
        ];
    }

    // $userIds = 1
    public function getUserAllConsume($uid): float
    {
        $ids = [$uid];
        $userIds = [$uid];
        for ($i = 0; $i < 100; $i++) {
            if (empty($userIds)) {
                break;
            }
            //继续查下一代
            $inIdStr = implode(',', $userIds);
            $sql = "select uid from eb_user where spread_uid in ({$inIdStr}) and pay_count > 0";
            $users = Db::query($sql);
            $userIds = array_column($users, 'uid'); // [2，3，4]
            $ids = array_merge($ids, $userIds);
        }
        return DB::table('eb_store_pink')
            ->whereIn('uid', $ids)
            ->where('pid', '>', 0)
            ->where('status', 2)
            ->sum('total_price');
    }


    /**
     * 获取用户记录里的积分总数和签到总数和余额变动总数
     * @param $uid
     * @return array
     */
    public function getUserBillCountData($uid) {
        /** @var UserBillServices $userBill */
        $userBill = app()->make(UserBillServices::class);
        $integral_count = $userBill->getIntegralCount($uid);
        $sign_count = $userBill->getSignCount($uid);
        $balanceChang_count = $userBill->getBrokerageCount($uid);
        return [$integral_count, $sign_count, $balanceChang_count];
    }

    public function read(int $uid) {
        $userInfo = $this->getUserInfo($uid);
        if (!$userInfo) {
            throw new AdminException('数据不存在');
        }
        $info = [
            'uid' => $uid,
            'userinfo' => $this->getUserDetailed($uid, $userInfo),
            'headerList' => $this->getHeaderList($uid, $userInfo),
            'count' => $this->getUserBillCountData($uid),
            'ps_info' => $userInfo
        ];
        return $info;
    }

    /**
     * 获取单个用户信息
     * @param $id 用户id
     * @return mixed
     */
    public function oneUserInfo(int $id, string $type) {
        switch ($type) {
            case 'spread':
                return $this->getSpreadList($id);
                break;
            case 'order':
                /** @var StoreOrderServices $services */
                $services = app()->make(StoreOrderServices::class);
                return $services->getUserOrderList($id);
                break;
            case 'integral':
                [$page, $limit] = $this->getPageValue();
                $points =  $this->getUserPointsLog($id, $page, $limit);
                foreach ($points['list'] as &$point) {
                    if (!empty($point['add_time'])) {
                        $point['add_time'] = date('Y-m-d H:i:s', intval($point['add_time']));
                    }
                }
                return $points;
                break;
            case 'sign':
                /** @var UserBillServices $services */
                $services = app()->make(UserBillServices::class);
                return $services->getSignList($id, [], 'title,number,mark,add_time');
                break;
            case 'coupon':
                /** @var StoreCouponUserServices $services */
                $services = app()->make(StoreCouponUserServices::class);
                return $services->getUserCouponList($id);
                break;
            case 'balance_change':
                /** @var UserBillServices $services */
                $services = app()->make(UserBillServices::class);
                return $services->getBrokerageList($id, [], 'title,type,number,balance,mark,pm,status,add_time');
                break;
            default:
                throw new AdminException('type参数错误');
        }
    }

    /**获取特定时间用户访问量
     * @param $time
     * @param $week
     * @return int
     */
    public function todayLastVisits($time, $week) {
        return $this->dao->todayLastVisit($time, $week);
    }

    /**获取特定时间新增用户
     * @param $time
     * @param $week
     * @return int
     */
    public function todayAddVisits($time, $week) {
        return $this->dao->todayAddVisit($time, $week);
    }

    /**
     * 用户图表
     */
    public function userChart() {
        $starday = date('Y-m-d', strtotime('-30 day'));
        $yesterday = date('Y-m-d');

        $user_list = $this->dao->userList($starday, $yesterday);
        $chartdata = [];
        $data = [];
        $chartdata['legend'] = ['用户数'];//分类
        $chartdata['yAxis']['maxnum'] = 0;//最大值数量
        $chartdata['xAxis'] = [date('m-d')];//X轴值
        $chartdata['series'] = [0];//分类1值
        if (!empty($user_list)) {
            foreach ($user_list as $k => $v) {
                $data['day'][] = $v['day'];
                $data['count'][] = $v['count'];
                if ($chartdata['yAxis']['maxnum'] < $v['count'])
                    $chartdata['yAxis']['maxnum'] = $v['count'];
            }
            $chartdata['xAxis'] = $data['day'];//X轴值
            $chartdata['series'] = $data['count'];//分类1值
        }
        $chartdata['bing_xdata'] = ['未消费用户', '消费一次用户', '留存客户', '回流客户'];
        $color = ['#5cadff', '#b37feb', '#19be6b', '#ff9900'];
        $pay[0] = $this->dao->count(['pay_count' => 0]);
        $pay[1] = $this->dao->count(['pay_count' => 1]);
        $pay[2] = $this->dao->userCount(1);
        $pay[3] = $this->dao->userCount(2);
        foreach ($pay as $key => $item) {
            $bing_data[] = ['name' => $chartdata['bing_xdata'][$key], 'value' => $pay[$key], 'itemStyle' => ['color' => $color[$key]]];
        }
        $chartdata['bing_data'] = $bing_data;
        return $chartdata;
    }

    /***********************************************/
    /************ 前端api services *****************/
    /***********************************************/

    /**
     * 用户信息
     * @param $info
     * @return mixed
     */
    public function userInfo($info) {
        /** @var UserBillServices $userBill */
        $userBill = app()->make(UserBillServices::class);
        $uid = (int)$info['uid'];
        $broken_time = intval(sys_config('extract_time'));
        $search_time = time() - 86400 * $broken_time;
        //改造时间
        $search_time = '1970/01/01' . ' - ' . date('Y/m/d H:i:s', $search_time);
        //可提现佣金
        //返佣 +
        $brokerage_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], $search_time);
        //退款退的佣金 -
        $refund_commission = (string)$userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 0], $search_time);
        $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
        if ($info['broken_commission'] < 0)
            $info['broken_commission'] = 0;
        //$info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
        $info['commissionCount'] = (float)$info['now_money'];
        $info['abc'] = "def";
        if ($info['commissionCount'] < 0)
            $info['commissionCount'] = 0;
        return $info;
    }

    /**
     * 个人中心
     * @param array $user
     */
    public function personalHome(array $user, $tokenData) {
        $userInfo = $user;
        $uid = (int)$user['uid'];
        /** @var StoreCouponUserServices $storeCoupon */
        $storeCoupon = app()->make(StoreCouponUserServices::class);
        /** @var UserBillServices $userBill */
        $userBill = app()->make(UserBillServices::class);
        /** @var UserExtractServices $userExtract */
        $userExtract = app()->make(UserExtractServices::class);
        /** @var StoreOrderServices $storeOrder */
        $storeOrder = app()->make(StoreOrderServices::class);
        /** @var UserLevelServices $userLevel */
        $userLevel = app()->make(UserLevelServices::class);
        /** @var StoreServiceServices $storeService */
        $storeService = app()->make(StoreServiceServices::class);
        /** @var SystemAttachmentServices $systemAttachment */
        $systemAttachment = app()->make(SystemAttachmentServices::class);
        /** @var WechatUserServices $wechatUser */
        $wechatUser = app()->make(WechatUserServices::class);
        $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
        $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
        $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
        $user['like'] = app()->make(StoreProductRelationServices::class)->getUserCollectCount($user['uid']);
        $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
        $user['notice'] = 0;
        $user['recharge'] = $userBill->getRechargeSum($uid);//累计充值
        $user['orderStatusSum'] = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
        $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
        $user['extractPrice'] = $user['brokerage_price'];//可提现
        $user['statu'] = (int)sys_config('store_brokerage_statu');
        if (!$user['is_promoter'] && $user['statu'] == 2) {
            $price = $storeOrder->sum(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']], 'pay_price');
            $status = is_brokerage_statu($price);
            if ($status) {
                $this->dao->update($uid, ['is_promoter' => 1], 'uid');
                $user['is_promoter'] = 1;
            } else {
                $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
                $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$price, 2);
            }
        }
        /** @var UserBrokerageFrozenServices $frozenPrices */
        $frozenPrices = app()->make(UserBrokerageFrozenServices::class);
        $user['broken_commission'] = array_bc_sum($frozenPrices->getUserFrozenPrice($uid));
        if ($user['broken_commission'] < 0)
            $user['broken_commission'] = 0;
        //$user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
        $user['commissionCount'] = (float)$user['now_money'];
        if ($user['commissionCount'] < 0)
            $user['commissionCount'] = 0;
        if (!sys_config('member_func_status'))
            $user['vip'] = false;
        else {
            $userLevel = $userLevel->getUerLevelInfoByUid($user['uid']);
            $user['vip'] = $userLevel ? true : false;
            if ($user['vip']) {
                $user['vip_id'] = $userLevel['id'] ?? 0;
                $user['vip_icon'] = $userLevel['icon'] ?? '';
                $user['vip_name'] = $userLevel['name'] ?? '';
            }
        }
        $user['yesterDay'] = $userBill->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
        $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
        $user['adminid'] = $storeService->checkoutIsService($uid);
        if ($user['phone'] && $user['user_type'] != 'h5') {
            $user['switchUserInfo'][] = $userInfo;
            $h5UserInfo = $this->dao->getOne(['account' => $user['phone'], 'user_type' => 'h5']);
            if ($h5UserInfo) {
                $user['switchUserInfo'][] = $h5UserInfo;
            }
        } else if ($user['phone'] && $user['user_type'] == 'h5') {
            $wechatUserInfo = $this->getOne([['phone', '=', $user['phone']], ['user_type', '<>', 'h5']]);
            if ($wechatUserInfo) {
                $user['switchUserInfo'][] = $wechatUserInfo;
            }
            $user['switchUserInfo'][] = $userInfo;
        } else if (!$user['phone']) {
            $user['switchUserInfo'][] = $userInfo;
        }
        $user['broken_day'] = (int)sys_config('extract_time');//佣金冻结时间
        //队列生成分销海报
//        $routine_poster = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine_poster_';
//        $wap_poster = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap_poster_';
//        $routine_poster_count = $systemAttachment->count(['like_name' => $routine_poster]);
//        $wap_poster_count = $systemAttachment->count(['like_name' => $wap_poster]);
//        if ($routine_poster_count == 0 || $wap_poster_count == 0) {
//            Queue::instance()->do('spreadPoster')->job(PosterJob::class)->data($user, $isssl)->push();
//        }
        $user['balance_func_status'] = (int)sys_config('balance_func_status', 0);
        $user['abc'] = 'def';
        return $user;
    }

    /**
     * 用户资金统计
     * @param int $uid
     */
    public function balance(int $uid) {
        $userInfo = $this->getUserInfo($uid);
        if (!$userInfo) {
            throw new ValidateException('数据不存在');
        }
        /** @var UserBillServices $userBill */
        $userBill = app()->make(UserBillServices::class);
        /** @var StoreOrderServices $storeOrder */
        $storeOrder = app()->make(StoreOrderServices::class);
        $user['now_money'] = $userInfo['now_money'];//当前总资金
        $user['recharge'] = $userBill->getRechargeSum($uid);//累计充值
        $user['orderStatusSum'] = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
        return $user;
    }

    /**
     * 获取用户发团点
     * @param int $uid
     * @return mixed
     */
    public function startNum(int $uid) {
        $userInfo = $this->getUserInfo($uid);
        if (!$userInfo) {
            throw new ValidateException('数据不存在');
        }
        return $userInfo['start_num'];
    }

    public function incStartNum(int $uid, $point) {
        $userInfo = $this->getUserInfo($uid);
        if (!$userInfo) {
            throw new ValidateException('数据不存在');
        }
        return $this->dao->incField($uid, 'start_num', intval($point));
    }

    public function decStartNum(int $uid, $point) {
        $userInfo = $this->getUserInfo($uid);
        if (!$userInfo) {
            throw new ValidateException('数据不存在');
        }
        return $this->dao->decField($uid, 'start_num', intval($point));
    }

    /**
     * 用户修改信息
     * @param Request $request
     * @return mixed
     */
    public function eidtNickname(int $uid, array $data) {
        if (!$this->getUserInfo($uid)) {
            throw new ValidateException('用户不存在');
        }
        if (!$this->dao->update($uid, $data, 'uid')) {
            throw new ValidateException('修改失败');
        }
        return true;
    }

    /**
     * 获取推广人排行
     * @param $data 查询条件
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRankList(array $data) {
        switch ($data['type']) {
            case 'week':
                $startTime = strtotime('this week');
                $endTime = time();
                break;
            case 'month':
                $startTime = strtotime('last month');
                $endTime = time();
                break;
        }
        [$page, $limit] = $this->getPageValue();
        $field = 't0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar';
        return $this->dao->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
    }

    /**
     * 静默绑定推广人
     * @param Request $request
     * @return mixed
     */
    public function spread(int $uid, int $spreadUid, $code) {
        $userInfo = $this->getUserInfo($uid);
        if (!$userInfo) {
            throw new ValidateException('数据不存在');
        }
        if ($code && !$spreadUid) {
            /** @var QrcodeServices $qrCode */
            $qrCode = app()->make(QrcodeServices::class);
            if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
                $spreadUid = $info['third_id'];
            }
        }
        $userSpreadUid = $this->dao->value(['uid' => $spreadUid], 'spread_uid');
        if (!$userInfo['spread_uid'] && $spreadUid != $uid && $userSpreadUid != $userInfo['uid']) {
            if (!$this->dao->update($uid, ['spread_uid' => $spreadUid, 'spread_time' => time()], 'uid')) {
                throw new ValidateException('绑定推广关系失败');
            }
            /** @var UserBillServices $userBill */
            $userBill = app()->make(UserBillServices::class);
            //邀请新用户增加经验
            $userBill->inviteUserIncExp((int)$spreadUid);
        }
        return true;
    }

    /**
     * 添加访问记录
     * @param Request $request
     * @return mixed
     */
    public function setVisit(array $data) {
        if (!$this->getUserInfo($data['uid'])) {
            throw new ValidateException('数据不存在');
        }
        $data['add_time'] = time();
        /** @var UserVisitServices $userVisit */
        $userVisit = app()->make(UserVisitServices::class);
        if ($userVisit->save($data)) {
            return true;
        } else {
            throw new ValidateException('添加访问记录失败');
        }
    }

    /**
     * 获取活动状态
     * @return mixed
     */
    public function activity() {
        /** @var StoreBargainServices $storeBragain */
        $storeBragain = app()->make(StoreBargainServices::class);
        /** @var StoreCombinationServices $storeCombinaion */
        $storeCombinaion = app()->make(StoreCombinationServices::class);
        /** @var StoreSeckillServices $storeSeckill */
        $storeSeckill = app()->make(StoreSeckillServices::class);
        $data['is_bargin'] = $storeBragain->validBargain() ? true : false;
        $data['is_pink'] = $storeCombinaion->validCombination() ? true : false;
        $data['is_seckill'] = $storeSeckill->getSeckillCount() ? true : false;
        return $data;
    }

    /**
     * 获取用户下级推广人
     * @param int $uid 当前用户
     * @param int $grade 等级  0  一级 1 二级
     * @param string $orderBy 排序
     * @param string $keyword
     * @return array|bool
     */
    public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '') {
        $data = [
            'total' => 0,
            'totalLevel' => count($this->getUserSpredadUids($uid, 1)),
            'list' => []
        ];
        $data['count'] = count($this->getUserSpredadUids($uid));
        $user = $this->getUserInfo($uid);
        if (!$user) {
            throw new ValidateException('数据不存在');
        }
        $userStair = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
        if (!count($userStair)) {
            return $data;
        }
        /** @var UserStoreOrderServices $userStoreOrder */
        $userStoreOrder = app()->make(UserStoreOrderServices::class);
        if ($grade == 0) {
            $data['total'] = count($userStair);
            $data['list'] = $userStoreOrder->getUserSpreadCountList($userStair, $orderBy, $keyword);
            return $data;
        }
        $userSecondary = $this->dao->getColumn([['spread_uid', 'IN', $userStair]], 'uid');
        $data['total'] = count($userStair);
        $data['totalLevel'] = count($userSecondary);
        $data['list'] = $userStoreOrder->getUserSpreadCountList($userSecondary, $orderBy, $keyword);
        return $data;
    }

    /**
     * 获取推广人uids
     * @param int $uid
     * @param bool $one
     * @return array
     */
    public function getUserSpredadUids(int $uid, int $type = 0) {
        $uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
        if ($type === 2) {
            return $uids;
        }
        if ($uids) {
            $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids]], 'uid');
            if ($type === 1) {
                return $uidsTwo;
            }
            if ($uidsTwo) {
                $uids = array_merge($uids, $uidsTwo);
            }
        }
        return $uids;
    }


    /**
     * 检测用户是否是推广员
     * @param int $uid
     * @param $user
     * @return bool
     */
    public function checkUserPromoter(int $uid, $user = []) {
        if (!$user) {
            $user = $this->getUserInfo($uid);
        }
        if (!$user) {
            return false;
        }
        //分销是否开启
        if (!sys_config('brokerage_func_status')) {
            return false;
        }
        /** @var StoreOrderServices $storeOrder */
        $storeOrder = app()->make(StoreOrderServices::class);
        $sumPrice = $storeOrder->sum(['uid' => $uid, 'paid' => 1, 'is_del' => 0], 'pay_price');//累计消费
        $store_brokerage_statu = sys_config('store_brokerage_statu');
        $store_brokerage_price = sys_config('store_brokerage_price');
        if ($user['is_promoter'] || $store_brokerage_statu == 2 || ($store_brokerage_statu == 3 && $sumPrice > $store_brokerage_price)) {
            return true;
        }
        return false;
    }

    /**
     * @param $uid
     * 更新用户级别
     * vip1  根据我的uid查下级，uids并count 如果大于5继续往下走，根据uids in查询下下级uids并count
     *       加上上面的count大于30 升级为vip1，小于30继续往下走，查这些uids的下级并count，再加，大于30升级为vip1 小于30升级失败
     * vip2  直接根据uid查下级uids 有3个1升级 没有往下 有3个升级 再没有继续下 有3个升级 没有终止
     * vip3  直接根据uid查下级uids 有3个2升级 没有往下 有3个升级 再没有继续下 有3个升级 没有终止
     */
    public function upUserLevel($uid) {
        //查询用户当前级别
        $sql = "select uid,`level` from eb_user where uid=$uid";
        $uInfo = Db::query($sql);
        $level = $uInfo[0]['level'];
        $lSql = "select id,grade from eb_system_user_level where grade in (1,2,3,4) and is_del=0 ";
        $lInfo = Db::query($lSql);
        $lMap = array_column($lInfo, 'id', 'grade');
        if ($level == $lMap[1] || $level == 0) {
            //验证升级vip1逻辑
            return $this->_checkUpVipOne($uid, $lMap[2]);
        } else if ($level == $lMap[2]) {
            //验证升级vip2逻辑
            return $this->_checkUpVipTwo($uid, $lMap[3], $lMap[2]);
        } else if ($level == $lMap[3]) {
            //验证升级vip3逻辑
            return $this->_checkUpVipThree($uid, $lMap[4], $lMap[3]);
        } else if ($level == $lMap[4]) {
            return ['status' => 1, 'msg' => '您已到最高级，不予升级~'];
        }else{
            return ['status' => 0, 'msg' => '操作失败'];
        }
    }

    /**
     * @param $uid
     * @param $level
     * @return bool
     */
    protected function updateVipLevel($uid, $level)
    {
        Db::startTrans();
        $sql = "update eb_user set `level`=$level where uid=$uid";
        $res[] = Db::execute($sql);
        $userLevelService = app()->make(UserLevelServices::class);
        $res[] = $userLevelService->setUserLevel($uid, $level);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    protected function getSubordinateUid($users, $count, $preId = 0)
    {
        if (empty($users)) {
            return [$users, $count];
        }
        $where = " and pay_count > 0 ";
        if ($preId > 0) {
            $where = " and `level` = {$preId} ";
        }
        //继续查下一代
        $idList = array_column($users, 'uid');
        putTempMsg('one' . json_encode($idList));
        $inIdStr = implode(',', $idList);
        $sql = "select count(1) as num from eb_user where spread_uid in ({$inIdStr}) {$where} ";
        $sum = Db::query($sql);
        $count += (int)$sum[0]['num'];
        $sql = "select uid from eb_user where spread_uid in ({$inIdStr})";
        $users = Db::query($sql);
        return [$users, $count];
    }

    //验证是否升级vip1
    protected function _checkUpVipOne($uid, $levelId, $preId = 0) {
        //查询直推人数
        $sql = "select uid from eb_user where spread_uid=$uid  and pay_count>0 ";
        $uInfo = Db::query($sql);
        $pCount = count($uInfo);
        if ($pCount < 5) {
            return ['status' => 0, 'msg' => '升级Vip1失败，还需' . (5 - $pCount) . "个直推账户"];
        }
        if ($pCount >= 30) {
            if ($this->updateVipLevel($uid, $levelId)) {
                return ['status' => 1, 'msg' => '升级Vip1成功！'];
            }
            return ['status' => 1, 'msg' => '升级Vip1失败~'];
        }

        for ($i=0; $i<=100; $i++) {
            [$uInfo, $pCount] = $this->getSubordinateUid($uInfo, $pCount, $preId);
            if ($pCount >= 30) {
                if ($this->updateVipLevel($uid, $levelId)) {
                    return ['status' => 1, 'msg' => '升级Vip1成功！'];
                    break;
                }
                return ['status' => 0, 'msg' => '升级Vip1失败~'];
                break;
            }
        }
        return ['status' => 0, 'msg' => '升级Vip1失败，还需' . (30 - $pCount) . "个伞账户"];

//        //继续查下一代
//        $twoIdList = array_column($uInfo, 'uid');
//        putTempMsg('one' . json_encode($twoIdList));
//        $twoIdStr = implode(',', $twoIdList);
//        $sql = "select uid from eb_user where spread_uid in ($twoIdStr)  and pay_count>0 ";
//        $twoUInfo = Db::query($sql);
//        $pCount += count($twoUInfo);
//        if ($pCount > 30) {
//            //更新vip级别 更新eb_user表  新增eb_user_level表
//            toUpdateOne:
//            $sql = "update eb_user set `level`=$levelId where uid=$uid and `level`=0";
//            $res[] = Db::execute($sql);
//            $userLevelService = app()->make(UserLevelServices::class);
//            $res[] = $userLevelService->setUserLevel($uid, $levelId);
//            if (!in_array(false, $res, true)) {
//                Db::commit();
//                return ['status' => 1, 'msg' => '升级Vip1成功！'];
//            }
//            Db::rollback();
//            return ['status' => 1, 'msg' => '升级Vip1失败~'];
//        } else {
//            //继续查下一代
//            $threeIdList = array_column($twoUInfo, 'uid');
//            $threeIdStr = implode(',', $threeIdList);
//            $sql = "select uid from eb_user where spread_uid in ($threeIdStr)  and pay_count>0 ";
//            $threeUInfo = Db::query($sql);
//            $pCount += count($threeUInfo);
//            if ($pCount > 30) {
//                goto toUpdateOne;
//            } else {
//                Db::rollback();
//                return ['status' => 0, 'msg' => '升级Vip1失败，还需' . (30 - $pCount) . "个伞账户"];
//            }
//        }
    }

    //验证是否升级vip2
    protected function _checkUpVipTwo($uid, $levelId, $preId = 0) {
        //查询直推人数
        $sql = "select count(1) as num from eb_user where spread_uid=$uid  and `level`=$preId ";
        $count = Db::query($sql);
        $pCount = $count[0]['num'];
        $sql = "select uid from eb_user where spread_uid=$uid";
        $uInfo = Db::query($sql);
        if ($pCount > 2) {
            if ($this->updateVipLevel($uid, $levelId)) {
                return ['status' => 1, 'msg' => '升级Vip2成功！'];
            }
            return ['status' => 0, 'msg' => '升级Vip2失败~'];
        }

        for ($i=0; $i<=100; $i++) {
            [$uInfo, $pCount] = $this->getSubordinateUid($uInfo, $pCount, $preId);
            if ($pCount > 2) {
                if ($this->updateVipLevel($uid, $levelId)) {
                    return ['status' => 1, 'msg' => '升级Vip2成功！'];
                    break;
                }
                return ['status' => 0, 'msg' => '升级Vip2失败~'];
                break;
            }
        }
        return ['status' => 0, 'msg' => '升级Vip2失败，还需直推' . (3 - $pCount) . '个Vip1账户'];

//        else {
//            //继续查下一代
//            if (!empty($uInfo)) {
//                $twoIdList = array_column($uInfo, 'uid');
//                $twoIdStr = implode(',', $twoIdList);
//                $sql = "select uid from eb_user where spread_uid in ($twoIdStr)  and `level`=$preId ";
//                $twoUInfo = Db::query($sql);
//                $pCount += count($twoUInfo);
//                if ($pCount > 2) {
//                    goto toUpdateTwo;
//                } else {
//                    if (!empty($twoUInfo)) {
//                        //继续查下一代
//                        $threeIdList = array_column($twoUInfo, 'uid');
//                        $threeIdStr = implode(',', $threeIdList);
//                        $sql = "select uid from eb_user where spread_uid in ($threeIdStr)  and `level`=$preId ";
//                        $threeUInfo = Db::query($sql);
//                        $pCount += count($threeUInfo);
//                        if ($pCount > 3) {
//                            goto toUpdateTwo;
//                        } else {
//                            Db::rollback();
//                            return ['status' => 0, 'msg' => '升级Vip2失败，还需直推' . (3 - $pCount) . "个Vip1账户"];
//                        }
//                    } else {
//                        Db::rollback();
//                        return ['status' => 0, 'msg' => '升级Vip2失败，还需直推' . (3 - $pCount) . "个Vip1账户"];
//                    }
//                }
//            } else {
//                Db::rollback();
//                return ['status' => 0, 'msg' => '升级Vip2失败，还需直推' . (3 - $pCount) . "个Vip1账户"];
//            }
//        }

    }

    //验证是否升级vip3
    protected function _checkUpVipThree($uid, $levelId, $preId = 0) {
        //查询直推人数
        $sql = "select count(1) as num from eb_user where spread_uid=$uid  and `level`=$preId ";
        $count = Db::query($sql);
        $pCount = $count[0]['num'];
        $sql = "select uid from eb_user where spread_uid=$uid";
        $uInfo = Db::query($sql);
        if ($pCount > 2) {
            if ($this->updateVipLevel($uid, $levelId)) {
                return ['status' => 1, 'msg' => '升级Vip3成功！'];
            }
            return ['status' => 0, 'msg' => '升级Vip3失败~'];
        }

        for ($i=0; $i<=100; $i++) {
            [$uInfo, $pCount] = $this->getSubordinateUid($uInfo, $pCount, $preId);
            if ($pCount > 2) {
                if ($this->updateVipLevel($uid, $levelId)) {
                    return ['status' => 1, 'msg' => '升级Vip3成功！'];
                    break;
                }
                return ['status' => 0, 'msg' => '升级Vip3失败~'];
                break;
            }
        }
        return ['status' => 0, 'msg' => '升级Vip3失败，还需直推' . (3 - $pCount) . '个Vip2账户'];

//        if ($pCount > 2) {
//            //更新vip级别 更新eb_user表  新增eb_user_level表
//            toUpdateThree:
//            $sql = "update eb_user set `level`=$levelId where uid=$uid and `level`=0";
//            $res[] = Db::execute($sql);
//            $userLevelService = app()->make(UserLevelServices::class);
//            $res[] = $userLevelService->setUserLevel($uid, $levelId);
//            if (!in_array(false, $res, true)) {
//                Db::commit();
//                return ['status' => 1, 'msg' => '升级Vip3成功！'];
//            }
//            Db::rollback();
//            return ['status' => 0, 'msg' => '升级Vip3失败~'];
//        } else {
//            //继续查下一代
//            if (!empty($uInfo)) {
//                $twoIdList = array_column($uInfo, 'uid');
//                $twoIdStr = implode(',', $twoIdList);
//                $sql = "select uid from eb_user where spread_uid in ($twoIdStr)  and `level`=$preId ";
//                $twoUInfo = Db::query($sql);
//                $pCount += count($twoUInfo);
//                if ($pCount > 2) {
//                    goto toUpdateThree;
//                } else {
//                    if (!empty($twoUInfo)) {
//                        //继续查下一代
//                        $threeIdList = array_column($twoUInfo, 'uid');
//                        $threeIdStr = implode(',', $threeIdList);
//                        $sql = "select uid from eb_user where spread_uid in ($threeIdStr)  and `level`=$preId ";
//                        $threeUInfo = Db::query($sql);
//                        $pCount += count($threeUInfo);
//                        if ($pCount > 3) {
//                            goto toUpdateThree;
//                        } else {
//                            Db::rollback();
//                            return ['status' => 0, 'msg' => '升级Vip3失败，还需直推' . (3 - $pCount) . "个Vip1账户"];
//                        }
//                    } else {
//                        Db::rollback();
//                        return ['status' => 0, 'msg' => '升级Vip3失败，还需直推' . (3 - $pCount) . "个Vip1账户"];
//                    }
//                }
//            } else {
//                Db::rollback();
//                return ['status' => 0, 'msg' => '升级Vip3失败，还需直推' . (3 - $pCount) . "个Vip1账户"];
//            }
//        }
    }


    /**
     * 提升用户等级
     *
     * @param int $orderId
     */
    public function _upUserLevel($orderId = 0) {
        //1.查询订单的信息详情
        $orderServices = app()->make(StoreOrderServices::class);
        $orderInfo = $orderServices->getOrderInfo($orderId);
        $userInfo = $this->getUserInfo($orderInfo->uid);
        //2.找到当前用户所有的平级人数
        $searchCondition = [
            ['level', '=', $userInfo->level],
            ['inv_code', '=', $userInfo->inv_code],
            ['pay_count', '>=', 1],
        ];
        $count = $this->getCount($searchCondition);
        if (!$count) {
            return;
        }
        //3.计算邀请人的会员等级
        $parentUserInfo = $this->dao->getUserSimple(['self_inv_code' => $userInfo->inv_code]);

        if (!$parentUserInfo) {
            return;
        }
        if (!$parentUserInfo) {
            return;
        }
        switch ($userInfo->level) {
            case 0:
                //5个直接推荐，伞下有30个已经购买过商品，当前会员等于0的
                if ($count >= 5 && $parentUserInfo->spread_count >= 30 && $parentUserInfo->level == 0) {
                    $this->dao->update($parentUserInfo->uid, ['level' => 1]);
                }
                break;
            case 1:
                //3个直推vip1，升级至vip2
                if ($count >= 3 && $parentUserInfo->level == 1) {
                    $this->dao->update($parentUserInfo->uid, ['level' => 2]);
                }
                break;
            case 2:
                //3个直推vip2，升级至vip3
                if ($count >= 3 && $parentUserInfo['level'] == 2) {
                    $this->dao->update($parentUserInfo->uid, ['level' => 3]);
                }
                break;
        }
    }

    public function addPayCount($uid) {
        $sql = "update eb_user set pay_count=pay_count+1 where uid=$uid";
        return Db::execute($sql);
    }

    /**
     * 佣金计算
     * @param int $uid
     * @param $orderPrice
     * @return bool
     */
    public function updateFirstInviteInfo(int $uid, $orderPrice) {
        //验证是否第一次参团
        $_pinkService = app()->make(StorePinkServices::class);
        $pinkCount = $_pinkService->getCount([['uid', '=', $uid], ['pid', '>', 0]]);
        $new_winning = get_pingtuan_config('new_winning');

        //获取推荐人
        $inviteInfo = $this->dao->getUserSimple(['uid' => $uid], 'spread_uid')->toArray();
        if (empty($inviteInfo)) return true;
        //记录佣金变动记录
        $inviteUserInfo = $this->dao->getUserSimple(['uid' => $inviteInfo['spread_uid']], 'now_money');
        if ($inviteUserInfo) {
            $inviteUserInfo = $inviteUserInfo->toArray();
        } else {
            return true;
        }
        $winMoney = 0;
        $res = [];
        if ($pinkCount < 2) {
            putTempMsg( '[' . date('Y-m-d H:i:s') . ']' . $pinkCount . '|' . $uid . '-' . $inviteInfo['spread_uid']);
            $billData = [
                'title' => '推荐下级奖励',
                'uid' => $inviteInfo['spread_uid'],
                'pm' => 1,
                'add_time' => time(),
                'category' => 'now_money',
                'type' => 'system_add',
                'number' => $new_winning,
                'link_id' => $uid,
                'balance' => bcadd((string)$inviteUserInfo['now_money'], (string)$new_winning, 2),
                'mark' => '推荐下级奖励' . $new_winning . '元'
            ];
            $winMoney += (int)$new_winning;
            $_userBillService = app()->make(UserBillServices::class);
            $res[] = $_userBillService->addBillData($billData);
        }
        $res[] = $this->dao->incField($inviteInfo['spread_uid'], 'now_money', $winMoney);//新增推荐奖励
        if (!in_array(false, $res, true)) {
            return true;
        }
        return false;
    }

    /**
     * vip返佣 向上查找3级 然后分3%流水 vp3平级分下级流水10%
     * @param int $uid
     * @param $orderPrice
     * @return bool|string
     */
    public function updateVipWinner(int $uid, $orderPrice) {
        Db::startTrans();
        $lSql = "select id,grade from eb_system_user_level where grade in (2,3,4)";
        $lInfo = Db::query($lSql);
        $lMap = array_column($lInfo, 'grade', 'id');
        $mySql = "select spread_uid,`level`,now_money from eb_user where uid=$uid";
        $myInfo = Db::query($mySql);
        $myInfo[0]['grade'] = (!empty($myInfo[0]['level']) && !empty($lMap[$myInfo[0]['level']])) ? $lMap[$myInfo[0]['level']] : 1;
        //上级
        $onePInfo = $twoPInfo = $threePInfo = [];
        if (!empty($myInfo[0])) {
            $pId = $myInfo[0]['spread_uid'];
            $onePSql = "select uid,spread_uid,`level`,now_money from eb_user where uid=$pId";
            $onePInfo = Db::query($onePSql);
            $onePInfo[0]['grade'] = (!empty($onePInfo[0]['level']) && !empty($lMap[$onePInfo[0]['level']])) ? $lMap[$onePInfo[0]['level']] : 1;
            if (!empty($onePInfo[0]) && !empty($onePInfo[0]['spread_uid'])) {
                $oneId = $onePInfo[0]['spread_uid'];
                $twoPSql = "select uid,spread_uid,`level`,now_money from eb_user where uid=$oneId";
                $twoPInfo = Db::query($twoPSql);
                $twoPInfo[0]['grade'] = (!empty($twoPInfo[0]['level']) && !empty($lMap[$twoPInfo[0]['level']])) ? $lMap[$twoPInfo[0]['level']] : 1;
                if (!empty($twoPInfo[0]) && !empty($twoPInfo[0]['spread_uid'])) {
                    $twoId = $twoPInfo[0]['spread_uid'];
                    $threePSql = "select uid,`level`,now_money from eb_user where uid=$twoId";
                    $threePInfo = Db::query($threePSql);
                    $threePInfo[0]['grade'] = (!empty($threePInfo[0]['level']) && !empty($lMap[$threePInfo[0]['level']])) ? $lMap[$threePInfo[0]['level']] : 1;
                }
            }
        }
        //保证传过去上三代级别 没有默认个普通会员
        $winUserInfo = [$myInfo[0], $onePInfo[0] ?? ['grade' => 1], $twoPInfo[0] ?? ['grade' => 1], $threePInfo[0] ?? ['grade' => 1]];
        $levelList = array_column($winUserInfo, 'grade');
        $winInfo = $this->getApplyMoney($orderPrice, $levelList);
        putTempMsg('用户id' . $uid . ',商品价格:' . $orderPrice . "，各等级信息" . json_encode($levelList) . "，返回流水信息:" . json_encode($winInfo));
        [$myWin, $oneWin, $twoWin, $threeWin, $equalWinInfo] = $winInfo;
        $billData = [];
        $res = [];
        if ($myWin > 0) {
            $billData[] = [
                'title' => 'VIP流水奖励',
                'uid' => $uid,
                'pm' => 1,
                'add_time' => time(),
                'category' => 'now_money',
                'type' => 'system_add',
                'number' => $myWin,
                'link_id' => $uid,
                'balance' => bcadd((string)$myInfo[0]['now_money'], (string)$myWin, 2),
                'mark' => 'VIP流水奖励' . $myWin . '元'
            ];
            $res[] = $this->dao->incField($uid, 'now_money', (float)$myWin);//新增流水奖励
        }
        if ($oneWin > 0 && !empty($onePInfo[0]['uid'])) {
            $billData[] = [
                'title' => 'VIP流水奖励',
                'uid' => $onePInfo[0]['uid'],
                'pm' => 1,
                'add_time' => time(),
                'category' => 'now_money',
                'type' => 'system_add',
                'number' => $oneWin,
                'link_id' => $uid,
                'balance' => bcadd((string)$onePInfo[0]['now_money'], (string)$oneWin, 2),
                'mark' => 'VIP流水奖励' . $oneWin . '元'
            ];
            $res[] = $this->dao->incField($onePInfo[0]['uid'], 'now_money', (float)$oneWin);//新增流水奖励
        }

        if ($twoWin > 0 && !empty($twoPInfo[0]['uid'])) {
            $billData[] = [
                'title' => 'VIP流水奖励',
                'uid' => $twoPInfo[0]['uid'],
                'pm' => 1,
                'add_time' => time(),
                'category' => 'now_money',
                'type' => 'system_add',
                'number' => $twoWin,
                'link_id' => $uid,
                'balance' => bcadd((string)$twoPInfo[0]['now_money'], (string)$twoWin, 2),
                'mark' => 'VIP流水奖励' . $twoWin . '元'
            ];
            $res[] = $this->dao->incField($twoPInfo[0]['uid'], 'now_money', (float)$twoWin);//新增流水奖励
        }

        if ($threeWin > 0 && !empty($threePInfo[0]['uid'])) {
            $billData[] = [
                'title' => 'VIP流水奖励',
                'uid' => $threePInfo[0]['uid'],
                'pm' => 1,
                'add_time' => time(),
                'category' => 'now_money',
                'type' => 'system_add',
                'number' => $threeWin,
                'link_id' => $uid,
                'balance' => bcadd((string)$threePInfo[0]['now_money'], (string)$threeWin, 2),
                'mark' => 'VIP流水奖励' . $threeWin . '元'
            ];
            $res[] = $this->dao->incField($threePInfo[0]['uid'], 'now_money', (float)$threeWin);//新增流水奖励
        }

        if (!empty($equalWinInfo)) {
            foreach ($equalWinInfo as $ind => $equalWinMoney) {
                $allAddMoney = bcadd((string)$equalWinMoney, (string)($winInfo[$ind]));
                $billData[] = [
                    'title' => 'VIP3平级流水奖励',
                    'uid' => $winUserInfo[$ind]['uid'],
                    'pm' => 1,
                    'add_time' => time(),
                    'category' => 'now_money',
                    'type' => 'system_add',
                    'number' => $equalWinMoney,
                    'link_id' => $uid,
                    'balance' => bcadd((string)$winUserInfo[$ind]['now_money'], (string)$allAddMoney, 2),
                    'mark' => 'VIP3平级流水奖励' . $equalWinMoney . '元'
                ];
                $res[] = $this->dao->incField($winUserInfo[$ind]['uid'], 'now_money', (float)$threeWin);//新增流水奖励
            }
        }
        if (!empty($billData)) {
            $_userBillService = app()->make(UserBillServices::class);
            $res[] = $_userBillService->addAllBillData($billData);
        }
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    protected function getApplyMoney($productPrice, $levelList) {
        [$my, $one, $two, $three] = $levelList;//[1,4,3,4]
        $pWinMap = [2 => 1, 3 => 2, 4 => 3];
        $totalPercent = 3;
        $myWinPercent = $pWinMap[$my] ?? 0;
        $oneWinPercent = $pWinMap[$one] ?? 0;
        $twoWinPercent = $pWinMap[$two] ?? 0;
        $threeWinPercent = $pWinMap[$three] ?? 0;
        $myWin = $oneWin = $twoWin = $threeWin = 0;
        if ($myWinPercent > 0) {
            $myWin = round(bcmul((string)round($myWinPercent / 100, 2), (string)$productPrice, 3),2);
            $totalPercent -= $myWinPercent;
        }

        if ($totalPercent > 0 && $oneWinPercent > 0) {
            $oneWinPercent = ($oneWinPercent - $myWinPercent) > 0 ? ($oneWinPercent - $myWinPercent) : 0;
            $oneWin = round(bcmul((string)round($oneWinPercent / 100, 2), (string)$productPrice, 3),2);
            $totalPercent -= $oneWinPercent;
        }

        if ($totalPercent > 0 && $twoWinPercent > 0) {
            $twoWinPercent = ($twoWinPercent - $oneWinPercent - $myWinPercent) > 0 ? ($twoWinPercent - $oneWinPercent - $myWinPercent) : 0;
            $twoWin = round(bcmul((string)round($twoWinPercent / 100, 2), (string)$productPrice, 3),2);
            $totalPercent -= $twoWinPercent;
        }

        if ($totalPercent > 0 && $threeWinPercent > 0) {
            $threeWinPercent = ($threeWinPercent - $twoWinPercent - $oneWinPercent - $myWinPercent) > 0 ? ($threeWinPercent - $twoWinPercent - $oneWinPercent - $myWinPercent) : 0;
            $threeWin = round(bcmul((string)round($threeWinPercent / 100, 2), (string)$productPrice, 3),2);
            $totalPercent -= $threeWinPercent;
        }
        $winInfo = [$myWin, $oneWin, $twoWin, $threeWin];
        //判断4出现在级别列表的个数
        $countInfo = array_count_values($levelList);
        $equalWinInfo = [];
        if (key_exists(4, $countInfo)) {
            if ($countInfo[4] > 1) {
                //至少出现2次 取最近2次的下标提取平级奖励
                $otherWinInfo = [];
                foreach ($levelList as $k => $v) {
                    if ($v == 4 && count($otherWinInfo) < 2) {
                        $otherWinInfo[$k] = $winInfo[$k];
                    } else if (count($otherWinInfo) > 1) {
                        break;
                    }
                }
                $endKeys = array_keys($otherWinInfo)[count($otherWinInfo) - 1];
                $startKeys = array_keys($otherWinInfo)[0];
                $equalWinInfo[$endKeys] = round($winInfo[$startKeys] * 10 / 100, 2);
            }
        }
        return [$myWin, $oneWin, $twoWin, $threeWin, $equalWinInfo];
    }

    /**
     * 开团点迁移
     * @param $fromId
     * @param $toId
     * @param $fromPoints
     * @param $toPoints
     * @param $movePoints
     * @return bool
     */
    public function moveStartPoint($fromId, $toId, $fromPoints, $toPoints, $movePoints) {
        Db::startTrans();
        //减开团点
        $res[] = $this->dao->decField($fromId, 'start_num', (int)$movePoints);
        //增开团点
        $res[] = $this->dao->incField($toId, 'start_num', (int)$movePoints);
        //新增迁移明细
        $fPointNum=(int)($fromPoints-$movePoints);
        $fTitle='转出发团点'.$movePoints;
        $tPointNum=(int)($toPoints+$movePoints);
        $tTitle='转入发团点'.$movePoints;
        $add_time=time();
        $addSql = "insert into eb_user_start_points (uid,link_id,pm,title,`number`,balance,mark,add_time) values 
($fromId,$toId,0,'".$fTitle."',$movePoints,$fPointNum,'发团点转出',$add_time),
($toId,$fromId,1,'".$tTitle."',$movePoints,$tPointNum,'发团点转入',$add_time)
";
        $res[] = Db::execute($addSql);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    /**
     * 余额转账
     * @param $fromId
     * @param $toId
     * @param $fromMoney
     * @param $toMoney
     * @param $moveMoney
     * @param $account
     * @return bool
     */
    public function moveUserMoney($fromId, $toId, $fromMoney, $toMoney, $moveMoney, $account) {
        Db::startTrans();
        //减开团点
        $res[] = $this->dao->decField($fromId, 'now_money', (int)$moveMoney);
        //增开团点
        $res[] = $this->dao->incField($toId, 'now_money', (int)$moveMoney);
        //新增迁移明细
        $fMoney=(int)($fromMoney-$moveMoney);
        $fMark='转出余额'.$moveMoney;
        $tMoney=(int)($toMoney+$moveMoney);
        $tMark='转入余额'.$moveMoney;
        $add_time=time();
        $addSql = "insert into eb_user_bill (title,uid,pm,add_time,category,`type`,`number`,link_id,balance,mark) values 
                   ('余额转出至{$account}',$fromId,0,$add_time,'now_money','pay_product',$moveMoney,$toId,$fMoney,'" . $fMark . "'),
                   ('余额由{$account}转入',$toId,1,$add_time,'now_money','system_add',$moveMoney,$fromId,$tMoney,'" . $tMark . "')";
        $res[] = Db::execute($addSql);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    /**
     * 获取迁移记录
     * @param $uid
     * @param $page
     * @param $pageNum
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPointsLog($uid, $page, $pageNum) {
        $limitStr = ($page - 1) * $pageNum . ',' . $pageNum;
        $countSql = "select count(1) as num from eb_user_start_points where uid=$uid";
        $countInfo = Db::query($countSql);
        $rs['count'] = $countInfo[0]['num'];
        $sql = "select * from eb_user_start_points where uid=$uid order by add_time desc limit $limitStr ";
        $rs['list'] = Db::query($sql);
        if (!empty($rs['list'])) {
            $linkIdList = array_column($rs['list'], 'link_id');
            $linkIdInfo = Db::table('eb_user')->field('uid,nickname,account')->where([['uid' ,'in', $linkIdList]])->select()->toArray();
            $idNameMap = array_column($linkIdInfo, 'nickname', 'uid');
            $accountMap = array_column($linkIdInfo, 'account', 'uid');
            foreach ($rs['list'] as &$v) {
                $v['nickname'] = $idNameMap[$v['link_id']]??'';
                $v['account'] = $accountMap[$v['link_id']]??'';
            }
        }
        return $rs;
    }

    /**
     * 获取积分记录
     * @param $uid
     * @param $page
     * @param $pageNum
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserPointsLog($uid, $page, $pageNum) {
        $limitStr = ($page - 1) * $pageNum . ',' . $pageNum;
        $countSql = "select count(1) as num from eb_user_integral_log where uid=$uid";
        $countInfo = Db::query($countSql);
        $rs['count'] = $countInfo[0]['num'];
        $sql = "select * from eb_user_integral_log where uid=$uid order by add_time desc limit $limitStr ";
        $rs['list'] = Db::query($sql);
        return $rs;
    }

    public function getRankOrderList($uInfo, $uid, $type, $page, $pageNum) {
        $limitStr = ($page - 1) * $pageNum . ',' . $pageNum;
        $countSql = "select count(1) as num from eb_user_integral_order where uid=$uid and status=$type";
        $countInfo = Db::query($countSql);
        $rs['count'] = $countInfo[0]['num'];
        $sql = "select * from eb_user_integral_order where uid=$uid and status=$type order by created desc limit $limitStr ";
        $rs['list'] = Db::query($sql);
        return $rs??[];
    }

    public function checkRankOrderIsExist($uid, $orderId) {
        $sql = "select * from eb_user_integral_order where uid=$uid and order_id='{$orderId}'";
        $info = Db::query($sql);
        return !empty($info) ? $info[0] : [];
    }

    //撤销 退积分
    public function cancelRankOrder($uInfo, $orderInfo) {
        //变更订单表状态
        Db::startTrans();
        $res[] = Db::table('eb_user_integral_order')
            ->where(['order_id' => $orderInfo['order_id'], 'uid' => $uInfo['uid']])
            ->save([
                'status' => 30,
                'cancel_time' => date('Y-m-d H:i:s'),
            ]);
        //退积分
        $sql = "update eb_user set points=points+" . (int)$orderInfo['consume_integral'] . " where uid={$uInfo['uid']}";
        $res[] = Db::execute($sql);

        //新增积分明细
        $integralData = [
            'uid' => $uInfo['uid'],
            'link_id' => $orderInfo['order_id'],
            'type' => 40,
            'pm' => 1,
            'title' => '撤销排行榜订单新增积分',
            'number' => $orderInfo['consume_integral'],
            'balance' => (int)$uInfo['points'] + $orderInfo['consume_integral'],
            'mark' => '撤销排行榜订单新增积分' . $orderInfo['consume_integral'] . '积分',
            'add_time' => time()
        ];
        $res[] = Db::table('eb_user_integral_log')->save($integralData);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    //撤销 退积分
    public function cancelRankMoneyOrder($ids) {
        //变更订单表状态
        $orders = Db::table('eb_user_integral_money_order')
            ->whereIn('id', $ids)
            ->whereIn('status', [1, 4])
            ->select()->toArray();
        Db::startTrans();
        $res[] = Db::table('eb_user_integral_money_order')
            ->whereIn('id', $ids)
            ->whereIn('status', [1, 4])
            ->save([
                'status' => 3,
                'cancel_time' => date('Y-m-d H:i:s'),
            ]);
        //退积分
        $integralData = [];
        foreach ($orders as $order) {
            $sql = "update eb_user set points=points+" . (int)$order['consume_integral'] . " where uid={$order['uid']}";
            $res[] = Db::execute($sql);
            $uInfo = Db::table('eb_user')->where('uid', $order['uid'])->find();
            //新增积分明细
            $integralData[] = [
                'uid' => $order['uid'],
                'link_id' => $order['order_id'],
                'type' => 40,
                'pm' => 1,
                'title' => '撤销消费分红订单新增积分',
                'number' => $order['consume_integral'],
                'balance' => (int)$uInfo['points'],
                'mark' => '撤销消费分红订单新增积分' . $order['consume_integral'] . '积分',
                'add_time' => time()
            ];
        }
        $res[] = Db::table('eb_user_integral_log')->insertAll($integralData);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    public function changeRankOrder($uInfo, $orderInfo,$configInfo){
        //变更订单表状态
        Db::startTrans();
        $res[] = Db::table('eb_user_integral_order')
            ->where(['order_id' => $orderInfo['order_id'], 'uid' => $uInfo['uid']])
            ->save([
                'status' => 20,
                'change_time' => date('Y-m-d H:i:s'),
            ]);
        //新增余额
        $addMoney=round(bcmul((string)round($configInfo['change_percent'] / 100, 2), (string)$orderInfo['consume_integral'], 3), 2);
        $sql = "update eb_user set now_money=now_money+" . $addMoney . " where uid={$uInfo['uid']}";
        $res[] = Db::execute($sql);

        //新增余额明细
        $billData = [
            'title' => '积分订单释放返余额',
            'uid' => $uInfo['uid'],
            'pm' => 1,
            'add_time' => time(),
            'category' => 'now_money',
            'type' => 'system_add',
            'number' => $addMoney,
            'link_id' => $orderInfo['order_id'],
            'balance' => bcadd((string)$uInfo['now_money'], (string)$addMoney, 2),
            'mark' => '积分订单释放返余额' . $addMoney . '元'
        ];
        $_userBillService = app()->make(UserBillServices::class);
        $res[] = $_userBillService->addAllBillData($billData);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    public function createRankOrder($uInfo, $configInfo) {
        Db::startTrans();
        $orderData = [
            'order_id' => 'RO' . getRandNumber(16),
            'uid' => $uInfo['uid'],
            'consume_integral' => $configInfo['limit_integral'],
            'status' => 40,
        ];
        $res[] = Db::table('eb_user_integral_order')->save($orderData);
        //更新用户积分
        $sql = "update eb_user set points=points-" . (int)$configInfo['limit_integral'] . " where uid={$uInfo['uid']}";
        $res[] = Db::execute($sql);

        //写入积分明细表
        $integralData = [
            'uid' => $uInfo['uid'],
            'link_id' => $orderData['order_id'],
            'type' => 30,
            'pm' => 0,
            'title' => '预约排行榜消耗积分',
            'number' => $configInfo['limit_integral'],
            'balance' => (int)$uInfo['points'] - $configInfo['limit_integral'],
            'mark' => '预约排行榜消耗' . $configInfo['limit_integral'] . '积分',
            'add_time' => time()
        ];
        $res[] = Db::table('eb_user_integral_log')->save($integralData);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    public function getCountRankMoneyOrder($uid) {
        return Db::table('eb_user_integral_money_order')
            ->where('uid', $uid)
            ->where('create_time', '>=', date('Y-m-d 00:00:00'))
            ->where('create_time', '<=', date('Y-m-d 23:59:59'))
            ->count();
    }

    public function getRankMoneyOrder($uid)
    {
        return Db::table('eb_user_integral_money_order')
            ->where('uid', $uid)
            ->order('id DESC')
            ->limit(0, 60)
            ->select()->toArray();
    }

    public function getGinvenRankMoney($uid, $linkId)
    {
        return Db::table('eb_user_bill')
            ->where('uid', $uid)
            ->where('title', '消费分红')
            ->where('link_id', $linkId)
            ->sum('number');
    }

    public function getMoveMoneyIsStart()
    {
        $config = Db::table('eb_combination_setting')
            ->where('name', 'move_money')
            ->find();
        return (int)$config['value'] === 2;
    }

    public function createRankMoneyOrder($uInfo, $configInfo) {
        Db::startTrans();
        $orderData = [
            'order_id' => 'RM' . getRandNumber(16),
            'uid' => $uInfo['uid'],
            'consume_integral' => $configInfo['limit_integral'],
            'given_money' => $configInfo['limit_integral'] * $configInfo['change_percent'],
            'status' => 1,
        ];
        $res[] = Db::table('eb_user_integral_money_order')->save($orderData);
        //更新用户积分
        $sql = "update eb_user set points=points-" . (int)$configInfo['limit_integral'] . " where uid={$uInfo['uid']}";
        $res[] = Db::execute($sql);

        //写入积分明细表
        $integralData = [
            'uid' => $uInfo['uid'],
            'link_id' => $orderData['order_id'],
            'type' => 30,
            'pm' => 0,
            'title' => '积分兑换消耗积分',
            'number' => $configInfo['limit_integral'],
            'balance' => (int)$uInfo['points'] - $configInfo['limit_integral'],
            'mark' => '积分兑换消耗' . $configInfo['limit_integral'] . '积分',
            'add_time' => time()
        ];
        $res[] = Db::table('eb_user_integral_log')->save($integralData);
        if (!in_array(false, $res, true)) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    public function getRankConfig() {
        $sql="select * from eb_rank_integral_config limit 1";
        $configInfo = Db::query($sql);
        return !empty($configInfo)?$configInfo[0]:[];
    }

    public function getDateRankOrderCount() {
        $nowDate = date('Y-m-d');
        $sql = "select count(*) as num from eb_user_integral_order where status in (10,20) and DATE_FORMAT(created,'%Y-%m-%d') >= '{$nowDate}' ";
        $countInfo = Db::query($sql);
        return !empty($countInfo) ? $countInfo[0]['num'] : 0;
    }
}
